/*
    This file is part of Epoch <http://code.google.com/p/epoch/>.
    
    Copyright (C) 2011  Adam Meily <meily.adam@gmail.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/


#ifndef LOGGER_H
#define LOGGER_H

#include <QtCore/QString>
#include <QtCore/QTextStream>
#include <QtCore/QIODevice>

#define INIT_LOG static Log *__file_log = Logger::getLog(__FILE__)
#define DEBUG(x) __file_log->debug(x, __LINE__)
#define ERROR(x) __file_log->error(x, __LINE__)
#define INFO(x)  __file_log->info(x, __LINE__)

class Logger;

class Log
{
public:
    Log(Logger **logger, const QString& name);
    
    void debug(const QString& msg, int line = -1);
    void error(const QString& msg, int line = -1);
    void info(const QString& msg, int line = -1);
    
private:
    QString m_name;
    Logger **m_logger;
};

class Logger
{
public:
    enum Level
    {
        Debug = 0,
        Info = 1,
        Error = 2
    };
    
    static Log* getLog(const QString& name);
    static Logger* instance();
    static Logger* init(QIODevice *stream, Level lvl);
    
    ~Logger();
    
    void log(Logger::Level lvl, const QString& name, const QString& msg, int line = -1);
    
private:
    static Logger *Instance;
    Logger(QIODevice *device, Logger::Level lvl);
    Level m_level;
    QTextStream *m_stream;
    QIODevice *m_device;
};

#endif // LOGGER_H
